package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	public class FrameNPCDialog extends JFrame
	{
		//members define
		private var panelImage:JPanel;
		private var panelContent:JPanel;
		private var txtContent:JTextArea;
		private var panelAnswers:JPanel;
		private var btnAnswer0:JLabelButton;
		private var btnAnswer1:JLabelButton;
		private var btnAnswer2:JLabelButton;
		private var btnAnswer3:JLabelButton;
		private var btnAnswer4:JLabelButton;
		private var btn_npc_dlg_complete:JButton;
		private var btn_npc_dlg_accepte:JButton;
		
		private var btn_close:JButton;
		public function FrameNPCDialog()
		{
			setClosable(false);
			setResizable(false);  
			setDragable(false);
			setBorder(new EmptyBorder());
			btn_close = new JButton();
			btn_close.setLocation(new IntPoint(453, 8));
			btn_close.setSize(new IntDimension(27, 27));
			btn_close.setPreferredSize(new IntDimension(27, 27));
			this.getTitleBar().setCloseButton(btn_close);
			getTitleBar().setMinimizeHeight(32);
			
			//component creation
			setSize(new IntDimension(508, 143));
			setPreferredSize(new IntDimension(508, 143));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			panelImage = new JPanel();
			panelImage.setLocation(new IntPoint(0, -103));
			panelImage.setSize(new IntDimension(207, 246));
			panelImage.setPreferredSize(new IntDimension(207, 246));
			
			panelContent = new JPanel();
			panelContent.setLocation(new IntPoint(217, -13));
			panelContent.setSize(new IntDimension(250, 52));
			panelContent.setPreferredSize(new IntDimension(250, 52));
			var layout1:CenterLayout = new CenterLayout();
			panelContent.setLayout(layout1);
			
			txtContent = new JTextArea();
			txtContent.setLocation(new IntPoint(217, -13));
			txtContent.setSize(new IntDimension(250, 21));
			txtContent.setWordWrap(true);
			
			panelAnswers = new JPanel();
			panelAnswers.setLocation(new IntPoint(227, 39));
			panelAnswers.setSize(new IntDimension(230, 80));
			panelAnswers.setPreferredSize(new IntDimension(230, 80));
			var layout2:SoftBoxLayout = new SoftBoxLayout();
			layout2.setAxis(AsWingConstants.VERTICAL);
			layout2.setAlign(AsWingConstants.TOP);
			layout2.setGap(0);
			panelAnswers.setLayout(layout2);
			
			btnAnswer0 = new JLabelButton();
			btnAnswer0.setSize(new IntDimension(260, 16));
			btnAnswer0.setPreferredSize(new IntDimension(260, 16));
			btnAnswer0.setText("label");
			btnAnswer0.setHorizontalAlignment(AsWingConstants.LEFT);
			
			btnAnswer1 = new JLabelButton();
			btnAnswer1.setLocation(new IntPoint(0, 18));
			btnAnswer1.setSize(new IntDimension(280, 16));
			btnAnswer1.setPreferredSize(new IntDimension(260, 16));
			btnAnswer1.setText("label");
			btnAnswer1.setHorizontalAlignment(AsWingConstants.LEFT);
			
			btnAnswer2 = new JLabelButton();
			btnAnswer2.setLocation(new IntPoint(0, 36));
			btnAnswer2.setSize(new IntDimension(280, 16));
			btnAnswer2.setPreferredSize(new IntDimension(260, 16));
			btnAnswer2.setText("label");
			btnAnswer2.setHorizontalAlignment(AsWingConstants.LEFT);
			
			btnAnswer3 = new JLabelButton();
			btnAnswer3.setLocation(new IntPoint(0, 54));
			btnAnswer3.setSize(new IntDimension(280, 16));
			btnAnswer3.setPreferredSize(new IntDimension(260, 16));
			btnAnswer3.setText("label");
			btnAnswer3.setHorizontalAlignment(AsWingConstants.LEFT);
			
			btnAnswer4 = new JLabelButton();
			btnAnswer4.setLocation(new IntPoint(0, 72));
			btnAnswer4.setSize(new IntDimension(260, 16));
			btnAnswer4.setPreferredSize(new IntDimension(260, 16));
			btnAnswer4.setText("label");
			btnAnswer4.setHorizontalAlignment(AsWingConstants.LEFT);
			
			btn_npc_dlg_complete = new JButton();
			btn_npc_dlg_complete.setLocation(new IntPoint(394, 113));
			btn_npc_dlg_complete.setSize(new IntDimension(73, 25));
			btn_npc_dlg_complete.setPreferredSize(new IntDimension(73, 25));
			btn_npc_dlg_complete.setText("label");
			
			btn_npc_dlg_accepte = new JButton();
			btn_npc_dlg_accepte.setLocation(new IntPoint(394, 113));
			btn_npc_dlg_accepte.setSize(new IntDimension(73, 25));
			btn_npc_dlg_accepte.setPreferredSize(new IntDimension(73, 25));
			btn_npc_dlg_accepte.setText("label");
			
			//component layoution
			getContentPane().append(panelImage);
			getContentPane().append(panelContent);
			getContentPane().append(panelAnswers);
			getContentPane().append(btn_npc_dlg_complete);
			getContentPane().append(btn_npc_dlg_accepte);
			
			panelContent.append(txtContent);
			
			panelAnswers.append(btnAnswer0);
			panelAnswers.append(btnAnswer1);
			panelAnswers.append(btnAnswer2);
			panelAnswers.append(btnAnswer3);
			panelAnswers.append(btnAnswer4);
			
			//------------------------------
			_arBtnAnswers.push(btnAnswer0);
			_arBtnAnswers.push(btnAnswer1);
			_arBtnAnswers.push(btnAnswer2);
			_arBtnAnswers.push(btnAnswer3);
			_arBtnAnswers.push(btnAnswer4);
			
			var filter:GlowFilter;
			var font:ASFont = new ASFont("宋体", 14, false, false, true, false);
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			
			//labelName.setFont(font);
			//labelName.setForeground(ASColor.WHITE);
			//labelName.setTextFilters(new Array(filter));
			
			for each(var btn:JLabelButton in _arBtnAnswers)
			{
				btn.setBackgroundDecorator(null);
				btn.setForeground(ASColor.YELLOW);
				btn.setRollOverColor(ASColor.RED);
				btn.setPressedColor(ASColor.BLUE);
				btn.setFont(font);
				btn.setTextFilters([filter]);
			}
			setImages();
			getContentPane().setClipMasked(false);
			txtContent.setPreferredWidth(panelContent.width);
			btnAnswer0.addActionListener(onBtnAnswer0);
			btnAnswer1.addActionListener(onBtnAnswer1);
			btnAnswer2.addActionListener(onBtnAnswer2);
			btnAnswer3.addActionListener(onBtnAnswer3);
			btnAnswer4.addActionListener(onBtnAnswer4);
			btn_npc_dlg_complete.addActionListener(onBtnComplete);
			btn_npc_dlg_accepte.addActionListener(onBtnAccepte);

		}

		private function onBtnAnswer0(e:Event):void
		{
			Client.getInstance().sendPacketLogic(901,[int(_currentCharNpc.userObject.npc_id),int(_arNPCDialogAnswer[0].nIndex)]);
			hide();
		}
		private function onBtnAnswer1(e:Event):void
		{
			Client.getInstance().sendPacketLogic(901,[int(_currentCharNpc.userObject.npc_id),int(_arNPCDialogAnswer[1].nIndex)]);
			hide();
		}
		private function onBtnAnswer2(e:Event):void
		{
			Client.getInstance().sendPacketLogic(901,[int(_currentCharNpc.userObject.npc_id),int(_arNPCDialogAnswer[2].nIndex)]);
			hide();
		}
		private function onBtnAnswer3(e:Event):void
		{
			Client.getInstance().sendPacketLogic(901,[int(_currentCharNpc.userObject.npc_id),int(_arNPCDialogAnswer[3].nIndex)]);
			hide();
		}
		private function onBtnAnswer4(e:Event):void
		{
			Client.getInstance().sendPacketLogic(901,[int(_currentCharNpc.userObject.npc_id),int(_arNPCDialogAnswer[4].nIndex)]);
			hide();
		}
		private function onBtnComplete(e:Event):void
		{
			Client.getInstance().sendPacketLogic(901,[int(_currentCharNpc.userObject.npc_id),int(m_nNPCDialogCompleteIndex)]);
			hide();
		}
		private function onBtnAccepte(e:Event):void
		{
			Client.getInstance().sendPacketLogic(901,[int(_currentCharNpc.userObject.npc_id),int(m_nNPCDialogAcceptIndex)]);
			hide();
		}
		private var _arBtnAnswers:Array = new Array();
		public function setImages():void
		{
			//setBackgroundDecorator(new SolidBackground(new ASColor(0x68878e,0.8)));
			//setLocationXY((Engine.W - width) / 2,(Engine.H - height)/2);
			var img_background:DisplayObject = Client.getUIElement("img_npc_dlg_bg") as DisplayObject
			setBackgroundDecorator(new AssetBackground(img_background));
			setSizeWH(img_background.width,img_background.height);
			setSize(new IntDimension(img_background.width, img_background.height));
			setPreferredSize(new IntDimension(img_background.width, img_background.height));
			pack();
			this.paintImmediately();
			//panelContent.pack();
			setLocationXY((Engine.W - img_background.width) / 2,(Engine.H - img_background.height)/2);
			btn_close.wrapSimpleButton(Client.getUIElement("btn_close") as SimpleButton);
			btn_npc_dlg_complete.wrapSimpleButton(Client.getUIElement("btn_npc_dlg_complete") as SimpleButton);
			btn_npc_dlg_accepte.wrapSimpleButton(Client.getUIElement("btn_npc_dlg_accepte") as SimpleButton);
			btn_npc_dlg_complete.setText("");
			btn_npc_dlg_accepte.setText("");
			panelImage.setEnabled(false);
			txtContent.setEditable(false);
			txtContent.setBackgroundDecorator(null);
			
			txtContent.setFont(new ASFont("宋体", 14, false, false, false, false));
			var filter:GlowFilter = new GlowFilter(0x000000, 1, 2, 2, 10);
			txtContent.filters = new Array(filter);
			//var filter:GlowFilter;
			//filter = new GlowFilter(0xdb9a8b, 1, 2, 2, 100);
			//filter = new GlowFilter(0xdb9a8b, 1, 2, 2, 100);
			txtContent.setForeground(ASColor.WHITE);
			//txtContent.filters = new Array(filter);
			txtContent.setWordWrap(true);
			//txtContent.addEventListener(TextEvent.LINK, linkHandler);
			//txtContent.addEventListener(MouseEvent.CLICK,onClick);
			var css:StyleSheet = new StyleSheet();
			//#925556
			//css.parseCSS("a:link { font-size: 13pt;  color: #ffff00; } a:hover { font-size: 13pt; color: #ff0000; }a:visited { font-size: 13pt; color: #cccccc; }");
			//txtContent.setCSS(css);
		}
		private var _mcNPCImage:Bitmap = null;
		public function setData():void
		{
			if(_mcNPCImage != null && this.containsChild(_mcNPCImage))
			{
				removeChild(_mcNPCImage);
				_mcNPCImage = null;
			}
			/*var mc:MovieClip = _currentCharNpc._mc_actions[Actions.Stand];
			if(mc == null)
			{
				hide();
				return;
			}
			//var imgSelf:JPanel = panelImage;
			mc.gotoAndStop(1);
			mc.scaleX = 2;
			mc.scaleY = 2;
			mc.x = panelContent.x - mc.width;//(width - mc.width)/2;//+mc.width/2;
			mc.y = height - mc.height;
			//imgSelf.setBackgroundDecorator(null);*/
			
			_mcNPCImage = _currentCharNpc.imageBitmap;
			if(_mcNPCImage != null)
			{
				//_mcNPCImage.scaleX = 1.0;
				//_mcNPCImage.scaleY = 1.0;
				_mcNPCImage.x = panelContent.x - _mcNPCImage.width;//(width - mc.width)/2;//+mc.width/2;
				_mcNPCImage.y = height - _mcNPCImage.height;
				addChild(_mcNPCImage);
				//_mcNPCImage.cacheAsBitmap = true;
			}
			this.setClipMasked(false);
			txtContent.setHtmlText('<font color=\"#d9941b\">'+_currentCharNpc.getName()+':</font>'+_strNPCDialogAsk);
			for each(var btn:JLabelButton in _arBtnAnswers)
			{
				btn.setText("");
				btn.setVisible(false);
			}
			var nIndex:int = 0;
			for each(var obj:Object in _arNPCDialogAnswer)
			{
				(_arBtnAnswers[nIndex] as JLabelButton).setText(obj.strAnswer);
				(_arBtnAnswers[nIndex] as JLabelButton).setVisible(true);
				nIndex++;
			}
			btn_npc_dlg_accepte.setVisible(m_nNPCDialogAcceptIndex != -1);
			btn_npc_dlg_complete.setVisible(m_nNPCDialogCompleteIndex != -1);
			panelContent.pack();
			txtContent.pack();
			this.pack();
			this.paintImmediately();
			
			if(_currentCharNpc.getPos().x > CharInfo.objSelf.getPos().x)
				_currentCharNpc.direction = Directions.Left;
			else
				_currentCharNpc.direction = Directions.Right;
		}
		public static var _frame:FrameNPCDialog = null;
		public static var _currentCharNpc:ObjectCharNPC = null;
		public static var _nNPCDialogNpcId:int = 0;
		public static var _strNPCDialogAsk:String = null;
		public static var _arNPCDialogAnswer:Array = new Array();
		public static var m_nNPCDialogAcceptIndex:int = 0;
		public static var m_nNPCDialogCompleteIndex:int = 0;

		public static function show():void
		{
			if(isShowing())
			{
				_frame.setData();
				return;
			}
			if(_frame == null)
			{
				_frame = new FrameNPCDialog();
				PanelMainHelp.getInstance().addChild(_frame);
			}
			_frame.show();
			PanelMainHelp.getInstance().bringToTop(_frame);
			_frame.setData();
			
		}
		public static function hide():void
		{
			if(isShowing())
			{
				if(_frame._mcNPCImage != null && _frame.containsChild(_frame._mcNPCImage))
				{
					_frame.removeChild(_frame._mcNPCImage);
					_frame._mcNPCImage = null;
				}
				_frame.hide();
			}
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
		
		public static function phNPCDialog_900(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phNPCDialog_900", msg);
			var nIndex:uint = 0;
			_nNPCDialogNpcId = msg[nIndex++];//说话者
			_strNPCDialogAsk = msg[nIndex++];//说话者
			if(_currentCharNpc == null || _currentCharNpc.userObject.npc_id != _nNPCDialogNpcId)
				return;
			var count:int = msg[nIndex++];
			_arNPCDialogAnswer.splice(0,_arNPCDialogAnswer.length);
			for(var i:int = 0; i < count; i++)
			{
				var obj:Object = new Object;
				obj.nIndex = msg[nIndex++];
				obj.strAnswer = msg[nIndex++];
				_arNPCDialogAnswer.push(obj);
			}
			m_nNPCDialogAcceptIndex = msg[nIndex++];
			m_nNPCDialogCompleteIndex = msg[nIndex++];
			show();
		}
		
		//private function linkHandler(linkEvent:TextEvent):void 
		//{
			//FrameInfo.showMessageDialog("","你已经到了"+linkEvent.text+"!");
		//	Client.getInstance().sendPacketLogic(901,[int(_currentCharNpc.userObject.npc_id),linkEvent.text]);
		//	hide();
		//}
		
		//public function onClick(e:MouseEvent):void 
		//{
			//if(txtContent.getHtmlText().search("event:") == -1)
		//		hide();
		//}
	}
}